package com.cindata

import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

@Transactional
class WorkflowInstanceNotificationService
{

    static scope = "singleton"

    // MessageService messageService
    ComponentService componentService

    def initWorkflowInstanceNotification(WorkflowInstanceStage workflowInstanceStage, WorkflowStage workflowStage)
    {
        def workflowInstanceNotification

        def workflowNotifications = WorkflowStageNotification.findAll("from WorkflowStageNotification where workflowStage.id = ${workflowStage?.id}")
        workflowNotifications?.each { workflowNotification ->
            if (workflowNotification?.role)
            {
                def users = UserRole.findAllByRole(workflowNotification?.role)*.user
                users?.each{ user ->
                    workflowInstanceNotification = WorkflowInstanceNotification.find("from WorkflowInstanceNotification where user.id = ${user?.id} and stage.id = ${workflowInstanceStage?.id} and messageTemplate.id = ${workflowNotification?.messageTemplate?.id} and cellphone = ${workflowNotification?.cellphone}")
                    if (!workflowInstanceNotification)
                    {
                        workflowInstanceNotification = new WorkflowInstanceNotification()
                        workflowInstanceNotification.stage = workflowInstanceStage
                        workflowInstanceNotification.user = user
                        workflowInstanceNotification.messageTemplate = workflowNotification?.messageTemplate
                        workflowInstanceNotification.toManager = workflowNotification?.toManager
                        workflowInstanceNotification.cellphone = workflowNotification?.cellphone
                        workflowInstanceNotification.save flush: true
                    }
                }
            } else
            {
                workflowInstanceNotification = WorkflowInstanceNotification.find("from WorkflowInstanceNotification where user.id is null and stage.id = ? and messageTemplate.id = ? and cellphone = ?", [workflowNotification?.messageTemplate?.id, workflowNotification?.messageTemplate?.id, workflowNotification?.cellphone])
                if (!workflowInstanceNotification)
                {
                    workflowInstanceNotification = new WorkflowInstanceNotification()
                    workflowInstanceNotification.stage = workflowInstanceStage
                    workflowInstanceNotification.messageTemplate = workflowNotification?.messageTemplate
                    workflowInstanceNotification.toManager = workflowNotification?.toManager
                    workflowInstanceNotification.cellphone = workflowNotification?.cellphone
                    workflowInstanceNotification.save flush: true
                }
            }
        }
    }

    def sendNotification(WorkflowInstance workflowInstance)
    {
        // def message
        // String cellphone
        // List<Reporting> reportings = []
        // String apiUrl = CreditReportProvider.find("from CreditReportProvider where code = 'PV'")?.apiUrl
        // if (workflowInstance?.status?.name == "Pending")
        // {
        //     def workflowInstanceNotifications = WorkflowInstanceNotification.findAll("from WorkflowInstanceNotification where stage.id = ${workflowInstance?.stage?.id}")
        //     workflowInstanceNotifications.each {
        //         try
        //         {
        //             if (it?.user)
        //             {
        //                 cellphone = it?.user?.cellphone
        //             } else if (it?.cellphone)
        //             {
        //                 cellphone = componentService.evaluate(it?.cellphone,workflowInstance)
        //             } else
        //             {
        //                 println "workflowInstance cellphone configuration error!"
        //             }
        //             message = componentService.evaluate(it?.cellphone,workflowInstance)

        //             messageService.sendMessage2(apiUrl, cellphone, message)

        //             if (it?.toManager && it?.user)
        //             {
        //                 reportings = Reporting.findAll("from Reporting where user.id = ?", [it?.user])
        //                 reportings?.each {
        //                     cellphone = it?.manager?.cellphone
        //                     messageService.sendMessage2(apiUrl, cellphone, message)
        //                 }
        //             }
        //         }
        //         catch (Exception e)
        //         {
        //             println "workflowInstance script execute error: " + e
        //         }
        //     }
        // }
    }
}
